<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
        }

        ul {
            font-size: 0px;
            margin: 30px auto;
            width: 180px;
            height: 180px;
            border: 1px solid black;
        }

        ul li {
            font-size: 20px;
            box-sizing: border-box;
            border: 1px solid red;
            height: 60px;
            width: 60px;
            display: inline-block;
            text-align: center;
            line-height: 60px;
            user-select: none;
        }
    </style>
</head>

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
        <li>9</li>
    </ul>
</body>
<script>
    var arr = []
    var countainer = document.querySelector("ul")
    for (var i = 0; i < 3; i++) {
        var row = []
        for (var j = 0; j < 3; j++) {
            row[j] = [countainer.children[i * 3 + j]]
        }
        arr[i] = row
    }
    arr[2][2][0].style.visibility = "hidden"
    countainer.onmousedown = function (event) {
        var event = event || window.event
        if (event.target.tagName == "LI") {
            var row = Math.floor((event.target.innerHTML - 1) / 3)
            var col = (event.target.innerHTML - 1) % 3
            for (var i = row - 1; i <= row + 1; i++) {
                for (var j = col - 1; j <= col + 1; j++) {
                    if (arr[i] && arr[i][j]) {
                        if ((i == row - 1 && j == col) || (i == row && j == col - 1) || (i == row && j == col + 1) || (i == row + 1 && j == col)) {
                            if (arr[i][j][0].style.visibility == "hidden") {
                                var change = arr[i][j]
                                var next = arr[i][j][0].nextElementSibling
                                console.log(arr[row][col][0].nextElementSibling.innerHTML)
                                arr[i][j][0].parentNode.insertBefore(arr[i][j][0], arr[row][col][0])
                                arr[i][j][0].parentNode.removeChild(arr[row][col][0])
                                arr[i][j][0].parentNode.insertBefore(arr[row][col][0], next)

                            }
                        }
                    }
                }
            }
        }


    }
</script>

</html>